﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;

namespace festivalDatabase
{
    public partial class editStages : userInterface
    {
        string oldStage;

        public editStages(dbAccessor db, string currentFestivalString)
        {
            InitializeComponent();
            dbAccess = db;
            getFestivals();
            setIcon();
            addStageRadioButton.Checked = true;
        }

        private void getFestivals()
        {
            SQLiteCommand query = dbAccess.getCommand();
            query.CommandText = "SELECT name, id " +
                                "FROM festival " +
                                "WHERE festival_type=@music;";
            SQLiteParameter param = new SQLiteParameter("@music", "Music");
            query.Parameters.Add(param);

            DataTable dt = dbAccess.executeQuery(query);

            try
            {
                foreach (DataRow row in dt.Rows)
                {
                    festivalComboBox.Items.Add(row["name"].ToString() + ", " + row["id"].ToString());
                }
            }
            catch
            {
                // do nothing
            }
        }

        /// <summary>
        /// Detects when anything in the stageListBox is clicked.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void stageListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            stageNameTextBox.Text = stageListBox.Text.ToString();
            oldStage = stageNameTextBox.Text;
        }

        /// <summary>
        /// Loads stages associated with a particular festival.
        /// </summary>
        /// <param name="selectedIndex"></param>
        public void updateStageList()
        {
            stageListBox.Items.Clear();

            SQLiteCommand query = dbAccess.getCommand();

            query.CommandText = "SELECT festival_id, stage_name " +
                                "FROM music_festival_has_stage " +
                                "WHERE festival_id=@festival_id;";
            
            SQLiteParameter param = new SQLiteParameter("@festival_id", getFestivalNumber(festivalComboBox.Text.ToString()));
            query.Parameters.Add(param);

            DataTable dt = dbAccess.executeQuery(query);

            try
            {
                foreach (DataRow row in dt.Rows)
                {
                    stageListBox.Items.Add(row["stage_name"].ToString());
                }
            }
            catch
            {
                // do nothing
            }
        }

        /// <summary>
        /// Adds a stage to the database.
        /// </summary>
        private void addStage()
        {
            if (stageNameTextBox.Text != "" && stageIsInListBox() == false)
            {
                SQLiteCommand nonQuery = dbAccess.getCommand();
                nonQuery.CommandText = "INSERT INTO music_festival_has_stage " +
                                       "VALUES (@festival_id, @stage_name);";

                SQLiteParameter param = new SQLiteParameter("@festival_id", getFestivalNumber(festivalComboBox.Text.ToString()));
                nonQuery.Parameters.Add(param);

                param = new SQLiteParameter("@stage_name", stageNameTextBox.Text);
                nonQuery.Parameters.Add(param);

                dbAccess.executeNonQuery(nonQuery);
                MessageBox.Show("Stage added successfully.");
                updateStageList();
                stageNameTextBox.Text = "";
            }
            else
            {
                MessageBox.Show("Stage already exists for this festival.");
            }
        }

        /// <summary>
        /// Detects whether a stage is already contained in the database or not.
        /// </summary>
        /// <returns></returns>
        bool stageIsInListBox()
        {
            foreach (String Item in stageListBox.Items)
            {
                if (Item.ToString() == stageNameTextBox.Text.ToString())
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Updates the stage information to the database.
        /// </summary>
        private void editStageInfo()
        {
            if (stageNameTextBox.Text != "" && stageIsInListBox() == false)
            {
                SQLiteCommand nonQuery = dbAccess.getCommand();

                nonQuery.CommandText = "UPDATE music_festival_has_stage " +
                                       "SET stage_name=@stage " +
                                       "WHERE festival_id=@festID AND stage_name=@oldStage;";
                SQLiteParameter param = new SQLiteParameter("@stage", stageNameTextBox.Text.ToString());
                nonQuery.Parameters.Add(param);
                param = new SQLiteParameter("@festID", getFestivalNumber(festivalComboBox.Text.ToString()));
                nonQuery.Parameters.Add(param);
                param = new SQLiteParameter("@oldStage", oldStage);
                nonQuery.Parameters.Add(param);

                dbAccess.executeNonQuery(nonQuery);

                nonQuery.CommandText = "UPDATE musician_plays " +
                                       "SET stage_name=@stage " +
                                       "WHERE festival_id=festID AND stage_name=@oldStage;";
                param = new SQLiteParameter("@stage", stageNameTextBox.Text.ToString());
                nonQuery.Parameters.Add(param);
                param = new SQLiteParameter("@festID", getFestivalNumber(festivalComboBox.Text.ToString()));
                nonQuery.Parameters.Add(param);
                param = new SQLiteParameter("@oldStage", oldStage);
                nonQuery.Parameters.Add(param);

                dbAccess.executeNonQuery(nonQuery);

                updateStageList();
                MessageBox.Show("Stage updated successfully");
            }
            else
                MessageBox.Show("Please enter a unique stage name.");
        }

        /// <summary>
        /// Deletes a stage from the database.
        /// </summary>
        private void removeStage()
        {
            SQLiteCommand nonQuery = dbAccess.getCommand();
            nonQuery.CommandText = "DELETE " +
                                   "FROM music_festival_has_stage " +
                                   "WHERE stage_name=@stage_name;";
            nonQuery.Parameters.Add("@stage_name", DbType.String);
            nonQuery.Parameters["@stage_name"].Value = stageNameTextBox.Text.ToString();
            dbAccess.executeNonQuery(nonQuery);

            nonQuery.CommandText = "DELETE FROM musician_plays " +
                                   "WHERE stage_name=@stage";
            SQLiteParameter param = new SQLiteParameter("@stage", stageNameTextBox.Text.ToString());
            nonQuery.Parameters.Add(param);
            dbAccess.executeNonQuery(nonQuery);

            updateStageList();
            stageNameTextBox.Text = "";
        }

        private void editStageRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            submitButton.Text = "Edit Stage";
            stageListBox.Enabled = true;
        }

        private void addStageRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            submitButton.Text = "Add Stage";
            stageListBox.Enabled = false;
        }

        private void deleteStageRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            submitButton.Text = "Delete Stage";
            stageListBox.Enabled = true;
        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            if (addStageRadioButton.Checked)
                addStage();
            else if (editStageRadioButton.Checked)
                editStageInfo();
            else
                removeStage();
        }

        private void festivalComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            submitButton.Enabled = true;
            updateStageList();
        }
    }
}
